package com.leo.erp.web.depothead;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.erp.dto.DepotHeadExecution;
import com.leo.erp.enums.DepotHeadStateEnum;
import com.leo.erp.service.DepotHeadService;
import com.leo.erp.util.GlobalRuntimeException;
import com.leo.erp.util.HttpServletRequestUtil;
import com.leo.erp.util.Tools;

@Controller
@RequestMapping(value = "/depotHead")
public class DepotHeadManagementController {
	@Autowired
	private DepotHeadService depotHeadService;
	
	@RequestMapping(value = "/findTotalPay")
	@ResponseBody
	public Map<String, Object> findTotalPay(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		int supplierId = HttpServletRequestUtil.getInt(request, "supplierId");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		String supType = HttpServletRequestUtil.getString(request, "supType");
		try {
			DepotHeadExecution de = depotHeadService.findTotalPay(supplierId, endTime, supType);
			modelMap.put("success", true);
			modelMap.put("getAllMoney", de.getTotalPay());
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", "获取数据失败");
		}

		return modelMap;
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String type = HttpServletRequestUtil.getString(request, "type");
		String subType = HttpServletRequestUtil.getString(request, "subType");
		String number = HttpServletRequestUtil.getString(request, "number");
		String beginTime = HttpServletRequestUtil.getString(request, "beginTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		String materialParam = HttpServletRequestUtil.getString(request, "materialParam");
		String depotIds = HttpServletRequestUtil.getString(request, "depotIds");
		
		int offset = HttpServletRequestUtil.getInt(request, "currentPage");
		int rows = HttpServletRequestUtil.getInt(request, "pageSize");
		
		DepotHeadExecution de = depotHeadService.list(type, subType, number, beginTime, endTime, materialParam, depotIds, offset, rows);
		if(de.getState() == DepotHeadStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("list", de.getDepotHeadVo4List());
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	/**
     * 统计今日销售额、本月销售额、本月进货额
     * @param request
     * @return
     */
	@RequestMapping(value = "/getBuyAndSaleStatistics")
	@ResponseBody
	public Map<String, Object> getBuyAndSaleStatistics(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		try {
			String today = Tools.getNow() + "00:00:00";
			String firstDay = Tools.getCurrentMonth() + "-01 00:00:00";
			BigDecimal todaySale = depotHeadService.getBuyAndSaleStatistics("出库", "销售", 1, today, Tools.getNow3());//今日销售出库
			BigDecimal todayRetailSale = depotHeadService.getBuyAndSaleRetailStatistics("出库", "零售", 0, today, Tools.getNow3()); //今日零售出库
            BigDecimal monthSale = depotHeadService.getBuyAndSaleStatistics("出库", "销售", 1,firstDay, Tools.getNow3()); //本月销售出库
            BigDecimal monthRetailSale = depotHeadService.getBuyAndSaleRetailStatistics("出库", "零售", 0,firstDay, Tools.getNow3()); //本月零售出库
            BigDecimal monthBuy = depotHeadService.getBuyAndSaleStatistics("入库", "采购", 1, firstDay, Tools.getNow3()); //本月采购入库
            modelMap.put("todaySale", todaySale.add(todayRetailSale));
            modelMap.put("thisMonthSale", monthSale.add(monthRetailSale));
            modelMap.put("thisMonthBuy", monthBuy);
            modelMap.put("success", true);
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", "获取数据失败");
		}
		
		return modelMap;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
